package com.xuecheng.content.api;

import com.xuecheng.base.exception.ValidationGroups;
import com.xuecheng.base.model.PageParams;
import com.xuecheng.base.model.PageResult;
import com.xuecheng.content.model.dto.AddCourseDto;
import com.xuecheng.content.model.dto.CourseBaseInfoDto;
import com.xuecheng.content.model.dto.EditCourseDto;
import com.xuecheng.content.model.dto.QueryCourseParamsDto;
import com.xuecheng.content.model.po.CourseBase;
import com.xuecheng.content.service.CourseBaseInfoService;
import com.xuecheng.content.util.SecurityUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;


/**
 * @Author 加油学习的梁仔
 * @Description:
 * @Date 2023/5/20 16:06
 * @Version 1.0
 */
@RestController
@Api(value = "课程信息编辑接口",tags ="课程信息编辑接口" )
public class CourseBaseInfoController {

    @Autowired
    CourseBaseInfoService courseBaseInfoService;

    /**
     * 细权限力度控制
     * 在资源服务集成Spring Security
     * 在需要授权的接口处使用@PreAuthorize("hasAuthority('权限标识符')")进行控制
     * 下边代码指定/course/list接口需要拥有xc_teachmanager_course_list 权限。
     * @param pageParams
     * @param queryCourseParams
     * @return
     */
    @PostMapping("/course/list")
    @PreAuthorize("hasAuthority('xc_teachmanager_course_list')")//拥有课程列表查询的权限方可访问    @ApiOperation(value = "课程查询接口")
    public PageResult<CourseBase> list(PageParams pageParams, @RequestBody(required = false) QueryCourseParamsDto queryCourseParams) {
        //取出用户身份
        SecurityUtil.XcUser user = SecurityUtil.getUser();
        //机构id
        String companyId = user.getCompanyId();
        return  courseBaseInfoService.queryCourseBaseList(Long.parseLong(companyId),pageParams, queryCourseParams);
     }


    /**
     * 1.保存课程基础信息
     * 2.保存课程收费信息
     * @param addCourseDto                                                                                              
     * @return
     */
    @PostMapping("/course")
    @ApiOperation(value = "新增课程基础信息")
    public CourseBaseInfoDto createCourseBase(@RequestBody @Validated({ValidationGroups.Inster.class}) AddCourseDto addCourseDto){
        //机构id，由于认证系统没有上线暂时硬编码
        Long companyId = 1232141425L;
        return courseBaseInfoService.createCourseBase(companyId,addCourseDto);
    }

    @GetMapping("/course/{courseId}")
    @ApiOperation(value = "根据id查询课程信息")
    public CourseBaseInfoDto  getCourseBaseById(@PathVariable Long courseId) {
        //取出当前用户身份
        Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        System.out.println(principal);

        return courseBaseInfoService.getCourseBaseInfo(courseId);
    }

    @ApiOperation("修改课程基础信息")
    @PutMapping("/course")
    public CourseBaseInfoDto modifyCourseBase(@RequestBody @Validated EditCourseDto editCourseDto){
        //机构id，由于认证系统没有上线暂时硬编码
        Long companyId = 1232141425L;
        return courseBaseInfoService.updateCourseBase(companyId,editCourseDto);
    }

    @ApiOperation("修改课程基础信息")
    @DeleteMapping("/{courseId}")
    public int deleteCource(@PathVariable Long courseId){
          return courseBaseInfoService.deleteCource(courseId);
    }
}
